package com.example.t1.activity;

import java.io.Serializable;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import com.example.t1.BaseViewActivity;
import com.example.t1.R;

/**
 * 
 * 名称：Activity7.java 描述：线程池ThreadPoolExecutor使用简介
 * 
 * @author 智迅 zhixun123@gmail.com
 * @date：2013-12-24 下午4:17:43
 */
public class Activity7 extends BaseViewActivity {
    private static int produceTaskSleepTime = 2;
    private static int produceTaskMaxNumber = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_activity7);
	// 构造一个线程池
	ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3,
		TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
		new ThreadPoolExecutor.DiscardOldestPolicy());

	for (int i = 1; i <= produceTaskMaxNumber; i++) {
	    try {
		// 产生一个任务，并将其加入到线程池
		String task = "task@ " + i;
		myLog("put " + task);
		threadPool.execute(new ThreadPoolTask(task));

		// 便于观察，等待一段时间
		Thread.sleep(produceTaskSleepTime);
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
    }
}

/**
 * 线程池执行的任务
 */
class ThreadPoolTask implements Runnable, Serializable {
    private static final long serialVersionUID = 0;
    private static int consumeTaskSleepTime = 2000;
    // 保存任务所需要的数据
    private Object threadPoolTaskData;

    ThreadPoolTask(Object tasks) {
	this.threadPoolTaskData = tasks;
    }

    public void run() {
	// 处理一个任务，这里的处理方式太简单了，仅仅是一个打印语句

	System.out.println(Thread.currentThread().getName());
	System.out.println("start .." + threadPoolTaskData);

	try {
	    // //便于观察，等待一段时间
	    Thread.sleep(consumeTaskSleepTime);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	threadPoolTaskData = null;
    }

}
